package com.seaking.service;

import com.seaking.dao.SchoolDao;
import com.seaking.pojo.School;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by wchha on 10/24 0024.
 */
@Service
public class SchoolService {

    @Autowired
    private SchoolDao schoolDao;

    public List<School> getSchool() {
        List<School> school = schoolDao.getSchool();
        if (school.size() > 0) {
            Iterator<School> iterator = school.iterator();
            while (iterator.hasNext()) {
                School next = iterator.next();
                if (next.getParentId() == 0) iterator.remove();
            }
        }
        return school;
    }

    /**
     * 递归列表
     *
     * @return
     */
    public List leftSchool() {
        List<School> lists = schoolDao.getSchool();
        if (lists.size() > 0) {
            List list2 = new ArrayList();
            List list = null;
            Boolean a = true;

            List list1 = listSet(lists, list2, list, 0, a);
            return list1;
        }
        return null;
    }

    /**
     * @param list1 总列表
     * @param list2 空集合
     * @param list  无对象引用 作为指向
     * @param i     父级标签
     * @param a     传入的布尔值
     * @return
     */
    public List listSet(List<School> list1, List list2, List list, Integer i, Boolean a) {
        School school = null;
        //对列表遍历
        Iterator<School> iterator = list1.iterator();
        while (iterator.hasNext()) {
            school = iterator.next();
            //当时父级行时，创建一个集合保存行 并将布尔值改为false
            if (school.getParentId() == 0 && a) {
                list = new ArrayList();
                list.add(school);
                a = false;
                //进入递归，将父级标签id传入 并将新创建的集合传入
                listSet(list1, list2, list, school.getId(), a);
                //当list部位空时将集合加入到空集合中 对list取空 对a取true
                if (list != null) list2.add(list);
                list = null;
                a = true;
                //不是父级id或者为false 集合添加对象
            } else if (school.getParentId() == i) {
                list.add(school);
            }
        }

        return list2;
    }
}
